import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import MainLayout from '@/layout/MainLayout.vue'

const routes = [
  {
    path: '/',
    redirect: '/login',
    hidden: true
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录', requiresAuth: false, hidden: true }
  },
  {
    path: '/admin',
    component: MainLayout,
    redirect: '/admin/home',
    meta: { 
      title: '管理后台', 
      requiresAuth: true,
      roles: [1] // 只有管理员可以访问
    },
    children: [
      {
        path: 'home',
        name: 'AdminHome',
        component: () => import('@/views/admin/home/index.vue'),
        meta: { 
          title: '管理首页', 
          icon: 'House',
          roles: [1] // 只有管理员可以访问
        }
      },
      {
        path:'user',
        name:'User',
        component: () => import('@/views/admin/user/index.vue'),
        meta:{
          title:'用户管理',
        icon:'User'
        }

      },
      {
        path: 'merchant',
        name: 'MerchantManage',
        meta: { title: '商家管理', icon: 'Shop' },
        children: [
          {
            path: 'list',
            name: 'MerchantList',
            component: () => import('@/views/admin/merchant/list/index.vue'),
            meta: { title: '商家列表' }
          },
          {
            path: 'verify',
            name: 'MerchantVerify',
            component: () => import('@/views/admin/merchant/verify/index.vue'),
            meta: { title: '商家审核' }
          }
        ]
      },
      {
        path: 'product',
        name: 'ProductManage',
        meta: { title: '商品管理', icon: 'Goods' },
        children: [
          {
            path: 'list/:merchantId?',
            name: 'AdminProductList',
            component: () => import('@/views/admin/product/list/index.vue'),
            meta: { 
              title: '商品列表',
              activeMenu: '/admin/product/list'
            }
          },
          {
            path: 'category',
            name: 'AdminProductCategory',
            component: () => import('@/views/admin/product/category/index.vue'),
            meta: { title: '分类管理' }
          },
          {
            path: 'comment',
            name: 'AdminProductComment',
            meta: { title: '评论管理' },
            children: [
              {
                path: 'list',
                name: 'AdminCommentList',
                component: () => import('@/views/admin/product/comment/index.vue'),
                meta: { title: '评论审核' }
              },
              {
                path: 'report',
                name: 'AdminCommentReport',
                component: () => import('@/views/admin/product/comment/report.vue'),
                meta: { title: '举报管理' }
              },
              {
                path: 'rule',
                name: 'AdminCommentRule',
                component: () => import('@/views/admin/product/comment/rule.vue'),
                meta: { title: '规则管理' }
              },
              {
                path: 'sensitive',
                name: 'AdminCommentSensitive',
                component: () => import('@/views/admin/product/comment/sensitive.vue'),
                meta: { title: '敏感词管理' }
              }
            ]
          }
        ]
      },
      {
        path: 'order',
        name: 'OrderManage',
        meta: { title: '订单管理', icon: 'List' },
        children: [
          {
            path: 'list',
            name: 'AdminOrderList',
            component: () => import('@/views/admin/order/list/index.vue'),
            meta: { title: '订单列表' }
          },
          {
            path: 'detail/:id',
            name: 'AdminOrderDetail',
            component: () => import('@/views/admin/order/detail/index.vue'),
            meta: { 
              title: '订单详情',
              activeMenu: '/admin/order/list'
            }
          }
        ]
      },
      {
        path: 'statistics',
        name: 'Statistics',
        component: () => import('@/views/admin/statistics/index.vue'),
        meta: { title: '统计分析', icon: 'DataLine' },
        children: [
          {
            path: 'product',
            name: 'ProductStatistics',
            component: () => import('@/views/admin/statistics/product/index.vue'),
            meta: { title: '商品统计' }
          },
          {
            path: 'user',
            name: 'UserStatistics',
            component: () => import('@/views/admin/statistics/user/index.vue'),
            meta: { title: '用户统计' }
          },
          {
            path: 'user/:id',
            name: 'UserAnalysis',
            component: () => import('@/views/admin/statistics/user/index.vue'),
            meta: { 
              title: '用户分析',
              activeMenu: '/admin/statistics/user'
            }
          },
          {
            path: 'comment',
            name: 'CommentStatistics',
            component: () => import('@/views/admin/statistics/comment/index.vue'),
            meta: { title: '评论统计' }
          },
          // {
          //   path: 'merchant',
          //   name: 'MerchantStatistics',
          //   component: () => import('@/views/admin/statistics/merchant/index.vue'),
          //   meta: { title: '商家统计' }
          // },
          {
            path: 'merchant/:id',
            name: 'MerchantAnalysis',
            component: () => import('@/views/admin/statistics/merchant/index.vue'),
            meta: { 
              title: '商家分析',
              activeMenu: '/admin/statistics/merchant'
            }
          },
          {
            path: 'recommendation',
            name: 'RecommendationManagement',
            component: () => import('@/views/admin/statistics/recommendation/index.vue'),
            meta: { title: '推荐系统管理' }
          }
        ]
      },
      {
        path: 'community',
        name: 'CommunityManage',
        component: () => import('@/views/admin/community/index.vue'),
        meta: { 
          title: '社区管理', 
          icon: 'ChatDotRound',
          roles: [1] // 只有管理员可以访问
        }
      }
    ]
  },
  {
    path: '/merchant',
    component: MainLayout,
    redirect: '/merchant/home',
    meta: { 
      title: '商家中心', 
      requiresAuth: true,
      roles: [2] // 只有商家可以访问
    },
    children: [
      {
        path: 'home',
        name: 'MerchantHome',
        component: () => import('@/views/merchant/home/index.vue'),
        meta: { 
          title: '商家首页', 
          icon: 'House',
          roles: [2] // 只有商家可以访问
        }
      },
      {
        path: 'product',
        name: 'MerchantProductManage',
        meta: { 
          title: '商品管理', 
          icon: 'Goods',
          roles: [2]
        },
        children: [
          {
            path: 'list',
            name: 'MerchantProductList',
            component: () => import('@/views/merchant/product/list/index.vue'),
            meta: { 
              title: '商品列表',
              roles: [2]
            }
          },
          {
            path: 'add',
            name: 'MerchantProductAdd',
            component: () => import('@/views/merchant/product/add/index.vue'),
            meta: { 
              title: '添加商品',
              roles: [2]
            }
          },
          {
            path: 'edit/:id',
            name: 'MerchantProductEdit',
            component: () => import('@/views/merchant/product/edit/index.vue'),
            meta: { 
              title: '编辑商品',
              roles: [2],
              activeMenu: '/merchant/product/list'
            }
          },
          {
            path: 'comment/:id',
            name: 'MerchantProductComment',
            component: () => import('@/views/merchant/product/comment/index.vue'),
            meta: { 
              title: '评论管理',
              roles: [2],
              activeMenu: '/merchant/product/list'
            }
          },
          {
            path: 'spec',
            name: 'MerchantProductSpec',
            component: () => import('@/views/merchant/product/spec/index.vue'),
            meta: { 
              title: '规格管理',
              roles: [2]
            }
          }
        ]
      },
      {
        path: 'order',
        name: 'MerchantOrderManage',
        meta: { 
          title: '订单管理', 
          icon: 'List',
          roles: [2]
        },
        children: [
          {
            path: 'list',
            name: 'MerchantOrderList',
            component: () => import('@/views/merchant/order/list/index.vue'),
            meta: { 
              title: '订单列表',
              roles: [2]
            }
          },
          {
            path: 'detail/:id',
            name: 'MerchantOrderDetail',
            component: () => import('@/views/merchant/order/detail/index.vue'),
            meta: { 
              title: '订单详情',
              roles: [2],
              activeMenu: '/merchant/order/list'
            }
          }
        ]
      },
      {
        path: 'statistics',
        name: 'MerchantStatistics',
        component: () => import('@/views/merchant/statistics/index.vue'),
        meta: { 
          title: '统计分析', 
          icon: 'DataLine',
          roles: [2]
        }
      }
    ]
  },
  {
    path: '/common',
    component: MainLayout,
    redirect: '/common/personal',
    meta: { 
      title: '通用功能', 
      requiresAuth: true,
      roles: [1, 2] // 管理员和商家都可以访问
    },
    children: [
      {
        path: 'personal',
        name: 'PersonalCenter',
        component: () => import('@/views/common/personal/index.vue'),
        meta: { 
          title: '个人中心', 
          icon: 'User',
          roles: [1, 2] // 管理员和商家都可以访问
        }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: { title: '404', requiresAuth: false, hidden: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  document.title = to.meta.title ? `${to.meta.title} - DMS系统` : 'DMS系统'

  // 处理登录后的重定向
  if (to.path === '/login' && userStore.token) {
    // 已登录用户访问登录页面时重定向到对应的首页
    const roleId = userStore.userInfo?.roleId
    if (roleId === 1) {
      next('/admin/home')
    } else if (roleId === 2) {
      next('/merchant/home')
    } else {
      next('/common/personal')
    }
    return
  }

  // 处理需要认证的路由
  if (to.meta.requiresAuth !== false) {
    if (!userStore.token) {
      ElMessage.warning('请先登录')
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
      return
    }

    // 角色权限检查
    const roleId = userStore.userInfo?.roleId
    if (to.meta.roles && !to.meta.roles.includes(roleId)) {
      ElMessage.error('没有访问权限')
      next(roleId === 1 ? '/admin/home' : '/merchant/home')
      return
    }
  }

  next()
})

export default router 